import { createRouter, createWebHistory } from 'vue-router'
import store from '../store'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import WeiboAnalysis from '../views/WeiboAnalysis.vue'
import DataAnalysis from '../views/DataAnalysis.vue'
import DataSourceManagement from '../views/DataSourceManagement.vue'
import Reports from '../views/Reports.vue'

const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home,
        meta: { requiresAuth: true },
        children: [
            {
                path: '',
                name: 'Dashboard',
                component: () => import('../views/Dashboard.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: 'profile',
                name: 'Profile',
                component: () => import('../views/Profile.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: 'data-sources',
                name: 'DataSources',
                component: DataSourceManagement,
                meta: { requiresAuth: true }
            },
            {
                path: 'data-collection',
                name: 'DataCollection',
                component: () => import('../views/DataCollection.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: 'data-analysis',
                name: 'DataAnalysis',
                component: DataAnalysis,
                meta: { requiresAuth: true }
            },
            {
                path: 'weibo-analysis',
                name: 'WeiboAnalysis',
                component: WeiboAnalysis,
                meta: { requiresAuth: true }
            },
            {
                path: 'visualization',
                name: 'Visualization',
                component: () => import('../views/Dashboard.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: 'reports',
                name: 'Reports',
                component: Reports,
                meta: { requiresAuth: true }
            },
            {
                path: 'settings',
                name: 'Settings',
                component: () => import('../views/Profile.vue'),
                meta: { requiresAuth: true }
            }
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: { guest: true }
    },
    {
        path: '/register',
        name: 'Register',
        component: Register,
        meta: { guest: true }
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
    const isAuthenticated = store.getters.isAuthenticated
    
    if (to.matched.some(record => record.meta.requiresAuth)) {
        if (!isAuthenticated) {
            next('/login')
        } else {
            next()
        }
    } else if (to.matched.some(record => record.meta.guest)) {
        if (isAuthenticated) {
            next('/')
        } else {
            next()
        }
    } else {
        next()
    }
})

export default router 